package com.itheima.leetcode.od.c.logicalsimulation;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * <h3>服务中心选址</h3>
 */
public class ServiceCenterSite {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        double[][] positions = new double[n][2];
        for (int i = 0; i < n; i++) {
            positions[i][0] = sc.nextInt();
            positions[i][1] = sc.nextInt();
        }

        System.out.println(getResult(n, positions));
    }

    public static double getResult(int n, double[][] positions) {
        ArrayList<Double> tmp = new ArrayList<>();
        for (double[] pos : positions) {
            tmp.add(pos[0]);
            tmp.add(pos[1]);
        }
        tmp.sort(Double::compareTo);

        double min = tmp.get(0);
        double max = tmp.get(tmp.size() - 1);
        double ans = Double.MAX_VALUE;

        for (double i = min; i <= max; i += 0.5) {
            double dis = 0;
            for (double[] pos : positions) {
                double l = pos[0];
                double r = pos[1];
                if (r < i) {
                    dis += i - r;
                } else if (i < l) {
                    dis += l - i;
                }
            }

            System.out.println(i + "\t" + dis);

            ans = Math.min(ans, dis);
        }

        return ans;
    }
}